var ScoreEffect = require('geoScoreEffectGroupController');
/**
 * 界面顶部的公共栏，包含退出、星星、设置按钮
 */
cc.Class({
    extends: cc.Component,

    properties: {
        scoreEffectPrefab:{
            default:null,
            type:cc.Prefab,
        },
        scoreEffectContainer:{
            default:null,
            type:cc.Node,
        },
        settingPrefab:{
            default:null,
            type:cc.Prefab,
        }
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.uiManager = require('geoUIManager').Singleton;
        // 创建得分效果器
        var effectNode = cc.instantiate(this.scoreEffectPrefab);
        this.scoreEffectContainer.addChild(effectNode);
        effectNode.setScale(1, 1);
        effectNode.position = new cc.Vec2();
        //
        this.scoreEffect = effectNode.getComponent(ScoreEffect);
    },
    /**
     * 当顶部回退按钮被点时触发
     */
    onQuitButtonClick(){
        var self = this;
        var paused = false
        // 暂停游戏
        var pauseGame = ()=>{
            cc.audioEngine.pauseAll();
            cc.director.pause();
            paused = true;
        };
        var resumeGame = ()=>{
            // 消除还未执行的延时方法
            if (paused == false)
                self.unschedule(pauseGame);
            // 恢复游戏
            cc.director.resume();
            cc.audioEngine.resumeAll();
        };
        var quitGame = ()=>{
            // window.history.back();
            // window.close();
            require('geoUtils').sys.quitGame();
        }
        //
        this.uiManager.openDialog(true)
            .setTitle('退出游戏')
            .setMessage('      退出游戏将不会保存刚才的游戏记录。')
            .addCloseButton('继续游戏', resumeGame)
            .addCloseButton('确定退出', quitGame);
        // 延时执行暂停，使得布局有时间实现
        this.scheduleOnce(pauseGame, 0.1);
    },
    /**
     * 当顶部设置按钮被点击时触发
     */
    onSettingButtonClick(){
        //
        var setting = cc.instantiate(this.settingPrefab);
        this.uiManager.openDialog(true)
            .setTitle('音量调节')
            .setContent(setting)
            .setContentSize(800, 600)
            .addCloseButton('关闭');
    },
    /**
     * 设置得分
     * @see geoScoreEffectGroupController.setScore()
     */
    setScore(score, complete, step){
        this.scoreEffect.setScore(score, complete, step);
    },
    loseScore(scores, complete, step){
        this.scoreEffect.loseScore(scores, complete, step);
    }
});
